<%@ page import="com.niit.work02.operatorFactory" %>
<%@ page import="com.niit.work02.operatorFactory" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Calculate</title>
</head>
<body>

<div>
    <div>
        <form action="" method="post">
            <table>
                <tr>
                    <td colspan="2" align="center">计算器</td>
                </tr>
                <tr>
                    <td>请输入数字 A:</td>
                    <td><input type="number" name="a"></td>
                </tr>
                <tr>
                    <td>请选择运算:</td>
                    <td>
                        <select name="operator">
                            <option value="1">+</option>
                            <option value="2">-</option>
                            <option value="3">*</option>
                            <option value="4">/</option>
                            <option value="5">%</option>
                            <option value="6">^</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>请输入数字 B:</td>
                    <td><input type="number" name="b"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="计算"></td>
                    <td><input type="reset" value="重置" onclick="clear()"></td>
                </tr>
            </table>
            <br>
            <div id="answer">结果：
                <%
                    if (request.getParameter("a") != null && request.getParameter("b") != null && request.getParameter("operator") != null) {
                        int a = Integer.parseInt(request.getParameter("a"));
                        int operator = Integer.parseInt(request.getParameter("operator"));
                        int b = Integer.parseInt(request.getParameter("b"));
                        operatorFactory operatorFactory = new operatorFactory();
                        int res = operatorFactory.getOperator(operator).calculate(a, b);
                        out.print(res);
                    }
                    else {
                        out.print("请输入全部选项");
                    }
                %>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    function clear() {
        document.getElementById("answer").innerHTML = "";
    }
</script>
</html>
